<?php

namespace app\api\controller;

use app\admin\model\About;
use app\admin\model\Banner;
use app\admin\model\Goods;
use app\admin\model\Nav;
use app\admin\model\News;
use app\admin\model\NewsCate;
use app\admin\model\Notice;
use app\common\controller\Api;
use think\Env;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('获取成功', [
            'lunbo' => $this->banner(),  //轮播
            'nav' => $this->nav(),        //金刚区
            'gonggao' => $this->notice(),  //公告
        ]);
    }


    /**
     * 搜索
     *
     */
    public function search()
    {
        $where = input('title');
        if (empty($where)) {
            $this->error('请输入商品名称！', [], 0);
        }
        $model = new Goods();
        $data = $model->where(['status' => Env::get('status.normal')])
            ->where(['type' => 1])
            ->where(function ($query) use ($where) {
                if ($where) {
                    $query->where('title', 'like', '%' . $where . '%');
                }
            })
            ->select();
        foreach ($data as &$v) {
            $image = explode(',', $v['image']);
            if (isset($image[0])) {
                $v['image'] = Env::get('domain.url') . $image[0];
            }
            $v->unsetAppend();
        }

        $this->success('获取成功', $data, 1);
    }


    /**
     * banner
     *
     */
    public function banner()
    {
        $model = new Banner();
        $data = $model->where(['status' => Env::get('status.normal')])
            ->where(['type' => 1])
            ->field('id,title,image')
            ->select();
        foreach ($data as &$v) {
            $v['image'] = Env::get('domain.url') . $v['image'];
        }
        return $data;
    }

    /**
     * 金刚区
     *
     */
    public function nav()
    {
        $model = new Nav();
        $data = $model->where(['status' => Env::get('status.normal')])
            //->where(['status' => env('status.normal')])
            ->field('id,title,image')
            ->select();
        foreach ($data as &$v) {
            $v['image'] = Env::get('domain.url') . $v['image'];
        }
        return $data;
    }


    /**
     * 金刚区详情
     *
     */
    public function nav_detail()
    {
        //  列表
        $type = input('type/d');
        if (empty($type)) {
            $this->error('请传入参数', $type, 0);
        }
        $model = new About();
        $list = $model->where(['type' => $type])
            ->field('id,title,image,content,createtime')
            ->select();
        foreach ($list as &$v) {
            $v['image'] = Env::get('domain.url') . $v['image'];
            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
        }

        // 详情
        $data_id = input('id/d');
        $detail = About::where(['status' => Env::get('status.normal'), 'id' => $data_id])->field('id,title,image,content')->find();
        $detail['image'] = Env::get('domain.url') . $detail['image'];
        $this->success('获取成功', $data = ['list' => $list, 'detail' => $detail], 1);
    }


    /**
     * 弹窗消息
     */
    public function alert()
    {
        $notice = !empty(Config('site.alert_notice')) ? Config('site.alert_notice') : '暂无公告！';
        $this->success('获取成功', $notice, 1);
    }

    /**
     * 公告
     *
     */
    public function notice()
    {
        $model = new Notice();
        $data = $model->where(['status' => Env::get('status.normal')])
            ->field('id,title,image')
            ->order('id desc')
            ->select();
        foreach ($data as &$v) {
            $v['image'] = Env::get('domain.url') . $v['image'];
        }
        return $data;
    }

    /**
     * 公告-列表+详情
     *
     */
    public function notice_list()
    {
        $model = new Notice();
        $data = $model->where(['status' => Env::get('status.normal')])
            ->field('id,title,image,createtime')
            ->select();
        foreach ($data as &$v) {
            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
            $v['image'] = Env::get('domain.url') . $v['image'];
        }
        $this->success('获取成功', $data, 1);

    }

    /**
     * 积分兑换
     *
     */
    public function exchange()
    {
        $model = new Goods();
        $type = input('type/d', 1);
        $data = $model->where(['type' => $type, 'is_top' => 1])
            ->field('id,title,image,score')
            ->limit(0, 3)
            ->select();
        foreach ($data as &$v) {
            $image = explode(',', $v['image']);
            if (isset($image[0])) {
                $v['image'] = Env::get('app.yuming') . $image[0];
            }
            $v->unsetAppend();
        }
        $this->success('获取成功', $data, 1);
    }

    /**
     * 新闻
     *
     */
    public function news()
    {
        //新闻分类
        $cate = NewsCate::where(['status' => Env::get('status.normal')])->field('id,title')->select();

        //新闻列表
        $data = input('id/d');
        $list = News::where(['status' => Env::get('status.normal'), 'news_cate_id' => $data])
            ->field('id,news_cate_id,title,image,createtime')
            ->select();
        foreach ($list as &$v) {
            $v['image'] = Env::get('domain.url') . $v['image'];
            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
        }

        //新闻详情
        $cate_id = input('cate_id/d');
        $detail = News::where(['status' => Env::get('status.normal'), 'id' => $cate_id])
            ->field('id,title,image,content')
            ->find();
        $detail['image'] = Env::get('domain.url') . $detail['image'];

        $this->success('获取成功', $data = ['cate' => $cate, 'list' => $list, 'detail' => $detail], 1);
    }

}
